package com.atguigu.userprofile.util;

import com.atguigu.userprofile.constant.ConstCode;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class ClickhouseUtil {

    /**
     * 负责到Clickhouse中执行删表和建表
     *
     */
    public static void executeSql(String sql ) {
        Connection connection = null ;
        PreparedStatement preparedStatement = null ;
        try {
            Class.forName("ru.yandex.clickhouse.ClickHouseDriver") ;
            connection = DriverManager.getConnection(MyPropsUtil.get(ConstCode.CK_URL));
            preparedStatement = connection.prepareStatement(sql);
            preparedStatement.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(preparedStatement != null && !preparedStatement.isClosed()){
                    preparedStatement.close();
                }
                if(connection != null && !connection.isClosed()){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
